package com.demo.controller;

import com.demo.config.DynamicDataSource;
import com.demo.entity.Course;
import com.demo.mapper.CourseMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by IntelliJ IDEA.
 * User: bj910
 * Date:2023-08-07
 * Time: 23:12
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/rds")
public class CourseController {
    @Resource
    CourseMapper courseMapper;

    @ResponseBody
    @RequestMapping("/queryCourse")
    public Object queryCourse(@RequestParam(value = "key",defaultValue = "R") String key){
        //通过key切换数据源。把key保存在threadLocal中
        DynamicDataSource.name.set(key);
        return courseMapper.selectList(null);
    }

    //get请求 参数拼在url中
    @ResponseBody
    @RequestMapping("/createCourse")
    public Object createCourse(@RequestParam(value = "key",defaultValue = "W") String key, Course course){
        DynamicDataSource.name.set(key);
        courseMapper.insert(course);
        return "insert success";
    }
}
